local sk__xianji = fk.CreateSkill {

  name = "sk__xianji",

  tags = {  },

}



sk__xianji:addEffect(fk.EventPhaseStart, {
  name = "sk__xianji",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__xianji.name) and player.phase == Player.Start and
      table.find(player.room:getOtherPlayers(player), function(p)
        return p:getMark("@sk__canshi") > p.maxHp and p:usedSkillTimes("sk__canshi", Player.HistoryGame) == 0
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.map(table.filter(player.room:getOtherPlayers(player), function(p)
      return p:getMark("@sk__canshi") > p.maxHp and p:usedSkillTimes("sk__canshi", Player.HistoryGame) == 0
    end), Util.IdMapper)
    local to = player.room:askForChoosePlayers(player, targets, 1, 1, "#sk__xianji-choose", sk__xianji.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local n = to:getMark("@sk__canshi")
    room:setPlayerMark(to, "@sk__canshi", 0)
    room:broadcastProperty(to, "MaxCards")
    room:changeMaxHp(player, n)
    if not player.dead and player:isWounded() then
      room:recover({
        who = player,
        num = math.min(n, player:getLostHp()),
        recoverBy = player,
        skillName = sk__xianji.name,
      })
    end
    if not player.dead and not to.dead then
      local skills1 = table.map(table.filter(player.player_skills, function(s)
        return s:isPlayerSkill(player) and s.visible
      end), function(s)
        return s.name
      end)
      local skills2 = table.map(table.filter(to.player_skills, function(s)
        return s:isPlayerSkill(player) and s.visible
      end), function(s)
        return s.name
      end)
      room:handleAddLoseSkills(player, "-"..table.concat(skills1, "|-").."|"..table.concat(skills2, "|"), nil, true, false)
      if not to.dead then
        room:handleAddLoseSkills(to, "-"..table.concat(skills2, "|-").."|"..table.concat(skills1, "|"), nil, true, false)
      end
    end
  end,
})

return sk__xianji